<template>
  <el-dialog v-model="isShowDialog"
             :before-close="handleClose"
             :title="title"
             draggable
             width="500px"
             append-to-body
  >
    <el-form :model="saveParams" ref="formRef" :disabled="isView" label-width="100px" :rules="rules">
      <el-form-item label="库区编码" prop="areaCode">
        <el-input v-model="saveParams.areaCode" @input="saveParams.areaCode=limitCharAndNumber(saveParams.areaCode)"  placeholder="仓库编码" clearable/>
      </el-form-item>
      <el-form-item label="库区名称" prop="areaName">
        <el-input v-model="saveParams.areaName" placeholder="库区名称" clearable/>
      </el-form-item>

      <el-form-item label="所属仓库" prop="warehouseId">
        <el-select
            :disabled="saveParams.areaId"
            clearable
            v-model="saveParams.warehouseId"
        >
          <template v-for="(item,index) in warehouseList" :key="index">
            <el-option :label="item.warehouseName" :value="item.warehouseId"></el-option>
          </template>
        </el-select>
      </el-form-item>

        <el-form-item label="区域类型">
          <el-select v-model="saveParams.zone" :disabled="saveParams.areaId" style="width: 100%" clearable
                     placeholder="区域类型">
            <el-option
                v-for="item in zone_type"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            />
          </el-select>
        </el-form-item>

      <el-form-item label="备注">
        <el-input v-model="saveParams.remark"   placeholder="备注" clearable/>
      </el-form-item>
      <el-form-item label="状态">
        <el-select

            style="width: 100%;"
            clearable
            placeholder="状态"
            v-model="saveParams.status"
        >
          <el-option label="启用" :value="0"></el-option>
          <el-option label="停用" :value="1"></el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="clickSave" v-if="!isView"> 保存 </el-button>
          <el-button @click="handleClose">取消</el-button>
        </span>
    </template>

  </el-dialog>
</template>

<script setup>
import {areaSave, warehouseGetFilter, warehouseSave} from "@/api/scApi";
import {getCurrentInstance, onMounted} from "vue";
import {limitCharAndNumber} from "@/utils/shuangcheng";
import {getCompanySelect, getWarehouseSelect} from "@/api/system/user";

const {proxy} = getCurrentInstance();
const { zone_type } = proxy.useDict("zone_type");
const props = defineProps({
  isShowDialog: {
    type: Boolean,
    default: false
  },
  title: {type: String},
  rowData: {type: Object},
  isView: {type: Boolean}
})
const emits = defineEmits(['cancel'])

function handleClose() {
  emits('cancel')
}

let saveParams = ref(JSON.parse(JSON.stringify(props.rowData)))
onMounted(() => {
  if (!saveParams.value.status) {
    saveParams.value.status =0
  }
  getWarehouse()

})
// 表单校验
const rules = ref({
  areaCode: [{required: true, message: "库区编码不能为空", trigger: "blur"}],
  areaName: [{required: true, message: "库区名称不能为空", trigger: "blur"}],
  companyId: [{required: true, message: "所属公司不能为空", trigger: "change"}],
  warehouseId: [{required: true, message: "所属仓库不能为空", trigger: "change"}],
});

function clickSave() {
  proxy.$refs.formRef.validate((valid, fileds) => {
    if (valid) {
      areaSave(saveParams.value).then(res => {
        if (res.code === 200) {
          proxy.$modal.msgSuccess('保存成功')
          emits('cancel', true)
        }
      })
    }
  })
}

//获取仓库下拉数据
let warehouseList = ref([])

function getWarehouse(){
  getWarehouseSelect({'companyId': saveParams.value.companyId}).then(res => {
    warehouseList.value = res.data
  })
}
</script>

<style scoped lang="scss">

</style>